package view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import controller.ACSystem;

/**
 * The main Frame
 * @author Bob Landowski
 * @author Eric Struck
 * 
 */
@SuppressWarnings("serial")
public class MainFrame extends JFrame {

	/** The current West JPanel used by the MainFrame JFrame */
	private Container currentWestPanel;

	/** The current East JPanel used by the MainFrame JFrame */
	private Container currentEastPanel;

	/** Local NewUserPane to be changed as needed. */
	private NewUserPane my_newUserPane;

	/** Local CalendarPane to be changed as needed. */
	private CalendarPane my_calendarPane;

	/** Local AuctionPane to be changed as needed. */
	private AuctionVerificationPane my_auctionVerificationPane;

	/** Local ItemPane to be changed as needed. */
	private ItemPane my_itemPane;

	/** Local BidderPane to be changed as needed. */
	private BidderPane my_bidderPane;

	/** Local ACEmployeePane to be changed as needed. */
	private ACEmployeePane my_ACEmployeePane;

	/** Local NonProfitPane to be changed as needed. */
	private NonProfitPane my_nonProfitPane;

	/** Local LogInPane to be changed as needed. */
	private LogInPane my_logInPane;

	/** Local AuctionInputPane to be changed as needed. */
	private AuctionInputPane my_auctionInputPane;

	/** Local ACDirectorPane to be changed as needed. */
	private ACDirectorPane my_ACDirectorPane;

	/** Local EastPanelACE to be changed as needed. */
	private EastPanelACE my_EastPanelACE;

	/** Local EastPanelACDir to be changed as needed. */
	private EastPanelACDir my_EastPanelACDir;

	/** Local EastPanelNP to be changed as needed. */
	public EastPanelNP my_EastPanelNP;

	/** Local EastPanelBidder to be changed as needed. */
	private EastPanelBidder my_EastPanelBidder;

	/** Local EastPanelBlank to be changed as needed. */
	private EastPanelBlank my_EastPanelBlank;

	/** Local JFrame to be changed as needed. */
	private JFrame my_mainframe;

	/** The Auction Pane*/
	private AuctionPane my_auctionPane;

	/** The item input pane */
	private ItemInputPane my_itemInputPane;

	/**
	 * The GUIController object that holds current state and acts as an
	 * interface between the GUI and the back end.
	 */
	private static ACSystem my_ACcontroller;

	/**
	 * 
	 * @param the_ac
	 */
	public MainFrame(final ACSystem the_ac) {
		my_ACcontroller = the_ac;
		createAllPanes();
		setFrame();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(400, 200, 500, 450);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');
		menuBar.add(fileMenu);

		JMenuItem exitMenu = new JMenuItem("Exit");
		exitMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		fileMenu.add(exitMenu);
		JMenu editMenu = new JMenu("Edit");
		menuBar.add(editMenu);

		JMenu helpMenu = new JMenu("Help");
		menuBar.add(helpMenu);

		JMenuItem versionMenu = new JMenuItem("Version");
		versionMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(my_mainframe,
						"Auction Central\nVersion number 1.328");
			}
		});
		helpMenu.add(versionMenu);

		JMenuItem aboutMenu = new JMenuItem("About");
		aboutMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(my_mainframe,
						"Auction Central\nAbdiel, Bob, Eric, Kevin");
			}
		});
		helpMenu.add(aboutMenu);
	}

	/**
	 * The west panel state.
	 */
	public void westPanelState(Object the_object) {

		this.remove(currentWestPanel);

		switch (my_ACcontroller.getWestPanel()) {
		case AUCTION_INPUT:
			my_auctionInputPane.getObjectData(the_object);
			currentWestPanel = my_auctionInputPane;
			break;
		case LOGIN:
			currentWestPanel = my_logInPane;
			break;
		case NEWUSER:
			currentWestPanel = my_newUserPane;
			break;
		case CALENDAR:
			currentWestPanel = my_calendarPane;
			break;
		case AUCTION:
			my_auctionPane.getObjectData(the_object);
			currentWestPanel = my_auctionPane;
			break;
		case AUCTIONVERIFICATION:
			my_auctionVerificationPane.getObjectData(the_object);
			currentWestPanel = my_auctionVerificationPane;
			break;
		case ITEM:
			my_itemPane.getObjectData(the_object);
			currentWestPanel = my_itemPane;
			break;
		case ITEMINPUT:
			my_itemInputPane.getObjectData(the_object);
			currentWestPanel = my_itemInputPane;
			break;
		case BIDDER:
			my_bidderPane.getObjectData(the_object);
			currentWestPanel = my_bidderPane;
			break;
		case ACEMPLOYEE:
			my_ACEmployeePane.getObjectData(the_object);
			currentWestPanel = my_ACEmployeePane;
			break;
		case ACDIRECTOR:
			my_ACDirectorPane.getObjectData(the_object);
			currentWestPanel = my_ACDirectorPane;
			break;
		case NONPROFIT:
			my_nonProfitPane.getObjectData(the_object);
			currentWestPanel = my_nonProfitPane;
			break;
		default:
			break;
		}

		getContentPane().add(currentWestPanel);
		setFrame();
	}

	/**
	 * the east panel state
	 * 
	 * @param the_object
	 */
	public void eastPanelState() {
		this.remove(currentEastPanel);

		switch (my_ACcontroller.getEastPanel()) {
		case UT_ACDIRECTOR:
			currentEastPanel = my_EastPanelACDir;
			break;
		case UT_ACEMPLOYEE:
			currentEastPanel = my_EastPanelACE;
			break;
		case UT_NONPROFIT:
			currentEastPanel = my_EastPanelNP;
			break;
		case UT_BIDDER:
			currentEastPanel = my_EastPanelBidder;
			break;
		case UT_BLANK:
			currentEastPanel = my_EastPanelBlank;
			break;
		default:
			break;
		}
		getContentPane().add(currentEastPanel);
		setFrame();

	}

	/**
	 * Method sets up the frame initially, instantiates the single GUIController
	 * Object used and initializes the first JPanel viewable to be the login
	 * window.
	 */
	private void setFrame() {
		// do not create new ACSystems in this class my_ACcontroller = new
		// ACSystem();
		this.setResizable(true);
		setTitle("Auction Central");
		getContentPane().add(currentWestPanel, BorderLayout.CENTER);
		getContentPane().add(currentEastPanel, BorderLayout.EAST);
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	/** The createAllPanes method */
	private void createAllPanes() {

		my_logInPane = new LogInPane(my_ACcontroller);
		my_newUserPane = new NewUserPane(my_ACcontroller);
		my_calendarPane = new CalendarPane(my_ACcontroller);
		my_auctionVerificationPane = new AuctionVerificationPane(
				my_ACcontroller);
		my_auctionPane = new AuctionPane(my_ACcontroller);
		my_auctionInputPane = new AuctionInputPane(my_ACcontroller);
		my_itemPane = new ItemPane(my_ACcontroller);
		my_bidderPane = new BidderPane(my_ACcontroller);
		my_ACEmployeePane = new ACEmployeePane(my_ACcontroller);
		my_nonProfitPane = new NonProfitPane(my_ACcontroller);
		my_ACDirectorPane = new ACDirectorPane(my_ACcontroller);
		my_EastPanelACE = new EastPanelACE(my_ACcontroller);
		my_EastPanelACDir = new EastPanelACDir(my_ACcontroller);
		my_EastPanelNP = new EastPanelNP(my_ACcontroller);
		my_EastPanelBidder = new EastPanelBidder(my_ACcontroller);
		my_EastPanelBlank = new EastPanelBlank(my_ACcontroller);
		my_itemInputPane = new ItemInputPane(my_ACcontroller);

		// This will be default first panel on launch.
		currentWestPanel = my_logInPane;
		currentEastPanel = my_EastPanelBlank;
	}

}